package org.jeecg.modules.fk.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.RestUtil;
import org.jeecg.modules.fk.entity.DjNsrxx;
import org.jeecg.modules.fk.vo.req.FkCompanyInfoRequest;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.mapper.SysUserRoleMapper;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

@Slf4j
@Api(tags = "风控公司")
@RestController
@RequestMapping("/fk/fkCompany")
public class FkCompanyController {
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private ISysUserService sysUserService;


    @AutoLog(value = "分页获取中介企业详情")
    @ApiOperation(value = "分页获取中介企业详情", notes = "分页获取中介企业详情")
    @PostMapping(value = "/listFkCompanyInfoDetail")
    public Result<Page<DjNsrxx>> listFkCompanyInfoDetail(@Valid @RequestBody FkCompanyInfoRequest request) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        // String roleNameListString = loginUser.getRoleNameListString();
        Set<String> roleNameListString = sysUserService.getUserRolesSet(loginUser.getUsername());
        //风控员工角色
        if (StringUtils.isNotBlank(roleNameListString.toString()) && roleNameListString.contains("admin")) {
            SysUserRole userRole = sysUserRoleMapper.getByRoleRelationByRoleCode("admin", loginUser.getId());
            if (userRole != null) {
                request.setSysUserId(loginUser.getId());
            }
        }

        Result<Page<DjNsrxx>> result = new Result<>();
        PageHelper.startPage(request.getPageNo(), request.getPageSize());
        Page<DjNsrxx> page = new Page<>(request.getPageNo(), request.getPageSize());
        // List<DjNsrxx> fkCompanyInfoResponses = fkCompanyInfoService.listFkCompanyInfoDetail(request);

        JSONObject jsonObject = RestUtil.post("http://39.108.178.94:8802/nsrxxcx/selectByNsrsbhOrShxydm", JSONObject.parseObject(JSON.toJSONString(request)));
        List<DjNsrxx> fkCompanyInfoResponses = JSONArray.parseArray(JSONObject.toJSONString(jsonObject), DjNsrxx.class);
        //风控员工角色
        if (StringUtils.isNotBlank(roleNameListString.toString()) && roleNameListString.contains("admin")) {
            SysUserRole userRole = sysUserRoleMapper.getByRoleRelationByRoleCode("admin", loginUser.getId());
            if (userRole != null) {
                fkCompanyInfoResponses.forEach(djNsrxx -> djNsrxx.setIsAdmin(0));
            }
            //fkCompanyInfoResponses.forEach(djNsrxx -> djNsrxx.setIsAdmin(0));
        } else {
            for (DjNsrxx djNsrxx : fkCompanyInfoResponses) {
                if (djNsrxx != null) {
                    djNsrxx.setIsAdmin(1);
                }
            }
        }
        /*fkCompanyInfoResponses.stream().forEach(one ->{
            if(one.getServiceTime()!=null) {
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                try {
                    Date parse = format.parse(one.getServiceTime());
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        });*/
        PageInfo<DjNsrxx> pageInfo = new PageInfo<>(fkCompanyInfoResponses);
        page.setRecords(pageInfo.getList());
        page.setCurrent(request.getPageNo());
        page.setTotal(pageInfo.getTotal());
        page.setSize(request.getPageSize());
        result.setSuccess(true);
        result.setResult(page);
        return result;
    }
}
